package com.pai4j.pgc.dao.community;
import com.pai4j.pgc.entity.community.CCQuestionEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 *
 * @author: CYM-pai
 * @date: 2025/10/27 14:38
 **/
@Repository
public interface ICCQuestionDAO extends JpaSpecificationExecutor<CCQuestionEntity>, JpaRepository<CCQuestionEntity, Long> {

    @Transactional
    @Modifying
    @Query("update CCQuestionEntity q set q.viewCount = q.viewCount + 1 where q.id = ?1")
    int increxQuestionViewCount(Long questionId);

    List<CCQuestionEntity> findAllByPathIdAndStatusOrderByIdDesc(Long pathId, Integer status);
    List<CCQuestionEntity> findAllByPathIdAndStatusNotOrderByIdDesc(Long pathId, Integer status);

    /**
     * 查询状态正常+已解决的置顶问题
     * @param size
     * @return
     */
    @Query(value = "select q.* from pai4j_question q where q.path_id = :pathId and q.top = true and q.status in (1, 2) " +
            "order by q.top_date desc limit :size", nativeQuery = true)
    List<CCQuestionEntity> findHomeRecommendTopQuestion(Long pathId, Integer size);

    /**
     * 查询状态正常+已解决的推荐问题
     * @param excludeIds
     * @param size
     * @return
     */
    @Query(value = "select q.* from cc_question q where q.path_id = :pathId and q.recommend = true " +
            "and q.status in (1, 2) and q.id not in :excludeIds " +
            "order by q.recommend_date desc limit :size", nativeQuery = true)
    List<CCQuestionEntity> findHomeRecommendRecommendQuestion(Long pathId, List<Long> excludeIds, Integer size);

    /**
     * 查询状态正常+已解决的悬赏问题
     * @param excludeIds
     * @param size
     * @return
     */
    @Query(value = "select q.* from cc_question q where q.path_id = :pathId and q.type = 2 and q.status in (1, 2) " +
            "and q.id not in :excludeIds " +
            "order by q.create_date desc limit :size", nativeQuery = true)
    List<CCQuestionEntity> findHomeRecommendRewardQuestion(Long pathId, List<Long> excludeIds, Integer size);

    /**
     * 查询状态正常+已解决的悬赏问题
     * @param excludeIds
     * @param size
     * @return
     */
    @Query(value = "select q.* from cc_question q where q.path_id = :pathId and q.status in (1, 2) " +
            "and q.id not in :excludeIds " +
            "order by q.create_date desc limit :size", nativeQuery = true)
    List<CCQuestionEntity> findHomeRecommendNewQuestion(Long pathId, List<Long> excludeIds, Integer size);
}
